package com.tencent.atk.activity;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.tencent.atk.Constants;
import com.tencent.atk.R;
import com.tencent.atk.adapter.AppListAdapter;
import com.tencent.atk.core.AppInfoManager;
import com.tencent.atk.model.AppInfo;
import com.tencent.atk.service.MainService;
import com.tencent.atk.util.ToastUtil;

/**
 * Created with eclipse User: liurenhui Date: 13-6-20 Time: PM1:40
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
	private static final String LOG_TAG = "MainActivity";
	private ListView listView;
	private Button button;
	private AppListAdapter appAdapter;
	private List<AppInfo> appList;
	private boolean isTesting = false;
	private int pid;
	private int uid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = (ListView) findViewById(R.id.app_list);
		button = (Button) findViewById(R.id.start_or_end);
		button.setOnClickListener(this);
		appList = AppInfoManager.getUserApp(this);
		appAdapter = new AppListAdapter(this);
		appAdapter.setItems(appList);
		listView.setAdapter(appAdapter);
		appAdapter.getSelectedId();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	private void waitForAppStart(String packageName) {
		boolean isProcessStarted = false;
		long startTime = System.currentTimeMillis();
		while (System.currentTimeMillis() < startTime + 20000) {
			ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
			List<RunningAppProcessInfo> processList = am
					.getRunningAppProcesses();
			for (RunningAppProcessInfo process : processList) {
				if ((process.processName != null)
						&& (process.processName.equals(packageName))) {
					pid = process.pid;
					Log.d(LOG_TAG, "pid:" + pid);
					uid = process.uid;
					if (pid != 0) {
						isProcessStarted = true;
						break;
					}
				}
			}
			if (isProcessStarted)
				break;
		}
	}

	@Override
	public void onClick(View view) {
		if (!isTesting) {
			if (appAdapter.getSelectedId() != -1) {
				int position = appAdapter.getSelectedId();
				AppInfo info = appList.get(position);
				Log.d("xxxxxxxxxxx", "msg:" + info);
				Intent intent = getPackageManager().getLaunchIntentForPackage(
						info.getPackageName());
				startActivity(intent);
				waitForAppStart(info.getPackageName());
				Intent service = new Intent(MainActivity.this, MainService.class);
				Log.e("puid", "msg: " + info.getUid());
				service.putExtra(Constants.UID, info.getUid());
				service.putExtra(Constants.PID, info.getPid());
				startService(service);
				isTesting = true;
				button.setText("停止测试");
			} else {
				ToastUtil.longToast(this, "请选择要测试的程序");
			}
		} else {
			isTesting = false;
			Intent stopService = new Intent(MainActivity.this, MainService.class);
			stopService(stopService);
			button.setText("开始测试");
		}
		
	}

}
